package com.zuipin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.BaseDictionaryData;
import com.zuipin.entity.product.ProductPrice;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Json;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IProductPriceService;
import com.zuipin.util.DateUtils;
import com.zuipin.vo.ProPriceVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 商品价格信息Controller
 *
 * @ClassName ProductPriceController
 * @author wangcongyi
 * @date 2017年5月3日 上午8:36:53
 */
@Controller
@RequestMapping(value = "/productPrice")
@Api(value = "商品价格信息", description = "商品价格信息-wangcongyi")
public class ProductPriceController {
	
	@Resource
	private IProductPriceService productPriceService;
	
	@ResponseBody
	@RequestMapping(value = "/proPriceManage", method = RequestMethod.POST)
	@ApiOperation(value = "商品价格管理(后台)", notes = "商品价格管理(后台)")
	@Valids({ @Valid(name = "price.productPrices.proId", required = true), @Valid(name = "price.productPrices.sysId", required = true),
			@Valid(name = "price.productPrices.priceType", required = true), @Valid(name = "price.productPrices.price", required = true) })
	public Result<?> updateOrCreateProPrice(@RequestBody @ApiParam ProPriceVo price) {
		Result<Integer> result = new Result<>();
		JSONObject params = new JSONObject();
		// 判断集合是否为空
		List<ProductPrice> productPrices = price.getProductPrices();
		if (productPrices.size() > 0) {
			// 获取商品信息id和系统id
			// Long sysId = productPrices.get(0).getSysId();
			Long proId = productPrices.get(0).getProId();
			// 获取当前时间
			String time = DateUtils.getCurrentDateTime();
			// 根据商品ID删除商品价格数据
			// params.put("sysId", sysId);
			params.put("proId", proId);
			productPriceService.deleteByProId(params);
			for (ProductPrice productPrice : productPrices) {
				productPrice.setCreateTime(time);
				productPrice.setUpdateTime(time);
			}
			// 批量添加商品价格数据
			int count = productPriceService.insertBatch(productPrices);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setData(count);
		} else {
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setData(null);
		}
		return result;
	}
	
	@Json(type = BaseDictionaryData.class, include = "id,dataName")
	@RequestMapping(value = "/getSetablePriceType", method = RequestMethod.GET)
	@ApiOperation(value = "获取商品该平台下未设置价格的类型(后台)", notes = "获取商品该平台下未设置价格的类型(后台)")
	@Valids({ @Valid(name = "price.proId", required = true), @Valid(name = "price.sysId", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "price.proId", value = "商品ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "price.sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"), })
	public Result<?> getSetablePriceType(@Inject @ApiIgnore ProductPrice price) {
		Result<List<BaseDictionaryData>> result = new Result<>();
		result.setData(productPriceService.getSetablePriceType(price));
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@Json(type = ProductPrice.class, include = "id,proId,sysId,priceType,price")
	@RequestMapping(value = "/getSettledPrice", method = RequestMethod.GET)
	@ApiOperation(value = "获取商品已设置价格的类型列表(后台)", notes = "获取商品已设置价格的类型列表(后台)")
	@Valids({ @Valid(name = "price.proId", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "price.proId", value = "商品ID", required = true, dataType = "long", paramType = "query"), })
	public Result<?> getSettledPrice(@Inject @ApiIgnore ProductPrice price) {
		Result<List<ProductPrice>> result = new Result<>();
		result.setData(productPriceService.getSettledPrice(price));
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
}
